﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using MvcJqGrid;
using PagedList;
using WebApp.Services;

namespace WebApp.Controllers
{
    public class BlockSeriesController : Controller
    {
        private BlockSeriesService _service;

        //
        // GET: /Block/
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult SeriesList(GridSettings gridSettings, string keyword)
        {
            _service = new BlockSeriesService();

            var realCount = 0;

            var datas = _service.BlockList(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          keyword,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }

            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.series_id,
                                cell = new[] {
                                            c.series_id.ToString(CultureInfo.InvariantCulture),
                                            c.series_name,
                                            "<div style='width: 55px; height: 20px; background-color: #" + "ccooff" + ";'></div>"
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
    }
}
